package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import model.CustomInfo;
import model.EventFieldsCondition;
import model.Field;
import dao.CustomInfoDao;
import dao.EventFieldsConditionDao;
import dao.FieldDao;
import utils.RequestUtil;

/**
 * Servlet implementation class FieldsServlet
 */
@WebServlet("/FieldsServlet")
public class FieldsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = RequestUtil.get(request.getParameter("method"));
		if (method.equals("getFieldListByFieldId")) {
			getFieldListByFieldId(request, response);
		}
	}


	


	private void getFieldListByFieldId(HttpServletRequest request,
			HttpServletResponse response) {
		String ueId = RequestUtil.get(request.getParameter("ueId"));
		if (ueId.equals(""))
			return;
		EventFieldsConditionDao efcDao = new EventFieldsConditionDao();
		List<EventFieldsCondition> efcList = new ArrayList<EventFieldsCondition>();
		FieldDao fDao = new FieldDao(); 
		efcList = efcDao.getAllByUeId(Integer.parseInt(ueId));
	
		List<Field> fieldList =fDao.getFieldListByFieldId((String.valueOf(efcList.get(0).getfId())));
		 fieldList =fDao.getFieldListbyTableId(String.valueOf(fieldList.get(0).gettId()));
		String json = JSONArray.fromObject(fieldList).toString();
	    try {
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
